package cn.stylefeng.guns.modular.fsjs.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.stylefeng.guns.core.util.ExcellUtil;
import cn.stylefeng.guns.core.util.ServerResponse;
import cn.stylefeng.guns.modular.system.model.Basemsg;
import cn.stylefeng.guns.modular.system.model.User;
import cn.stylefeng.guns.modular.system.dao.BasemsgMapper;
import cn.stylefeng.guns.modular.fsjs.service.IBasemsgService;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2019-03-05
 */
@Service
public class BasemsgServiceImpl extends ServiceImpl<BasemsgMapper, Basemsg> implements IBasemsgService {
	String mainPath="";
	@Autowired
	private BasemsgMapper basemsgMapper;
	
	@Override
	@Transactional(propagation =Propagation.REQUIRED,rollbackFor=Exception.class,isolation = Isolation.READ_COMMITTED)
	public Object pushMsg(HttpServletRequest request,Basemsg basemsg,User user) {
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		basemsg.setTs(sf.format(new Date()));
		if(basemsgMapper.getIfComit(user)>0){
			return ServerResponse.createByErrorMessage("年报数据已经提交，无法修改");
		}
		mainPath=request.getSession().getServletContext().getRealPath("/")+"\\static\\excell\\";
		if( StringUtils.isNotBlank(basemsg.getUnitId())&&
			basemsg.getRecYear()!=null&&
			StringUtils.isNotBlank(basemsg.getUnitName())&&
			StringUtils.isNotBlank(basemsg.getUnitType())&&
//			StringUtils.isNotBlank(basemsg.getFactoryName()())&&
			StringUtils.isNotBlank(basemsg.getOrgCode())&&
			StringUtils.isNotBlank(basemsg.getOwnership())&&
			StringUtils.isNotBlank(basemsg.getUserlevel())&&
//			StringUtils.isNotBlank(basemsg.getManagerDepartment())&&
			StringUtils.isNotBlank(basemsg.getTown())&&
			StringUtils.isNotBlank(basemsg.getAddr())&&
			StringUtils.isNotBlank(basemsg.getContacts())&&
			StringUtils.isNotBlank(basemsg.getTel())&&
			StringUtils.isNotBlank(basemsg.getIsWaterPlan())&&
			StringUtils.isNotBlank(basemsg.getDistrict())&&
			StringUtils.isNotBlank(basemsg.getGBIndustry())&&
			StringUtils.isNotBlank(basemsg.getMainIndustry())&&
			StringUtils.isNotBlank(basemsg.getIndustryType())&&
			StringUtils.isNotBlank(basemsg.getCoolType())&&
			StringUtils.isNotBlank(basemsg.getRecycleDevice())&&
			StringUtils.isNotBlank(basemsg.getCivicismRecycle())&&
			StringUtils.isNotBlank(basemsg.getRainDevice())&&
			StringUtils.isNotBlank(basemsg.getWaterSaveUint())&&
			basemsg.getAllrow1()!=null&&
			basemsg.getAllrow2()!=null&&
			basemsg.getAllrow3()!=null&&
			basemsg.getAllrow4()!=null&&
			StringUtils.isNotBlank(basemsg.getUnitHead())&&
			StringUtils.isNotBlank(basemsg.getOperater())
			){
			basemsgMapper.delTempMsg(basemsg);
			if(basemsgMapper.getCount(basemsg)==0&&basemsgMapper.pushMsg(basemsg)>0){
				//初始化excell成功
				ServerResponse cf=(ServerResponse)this.createFile(basemsg);
				if(cf.getCode()==200){
					//
					List<Basemsg> bsg=this.getMsgByAccountExcell(basemsg);
					if(bsg.size()==1){
						
						ServerResponse sm=(ServerResponse)this.stuffMsg(bsg.get(0),user);
						if(sm.getCode()==200){
							ServerResponse fn=(ServerResponse)this.toPDF(basemsg);
							if(fn.getCode()==200){
								this.delMsg(basemsg);
								return ServerResponse.createBySuccessMessage("保存成功");
							}else{
								return fn;
							}
						}else{
							return sm;
						}
						
					}else{
						return ServerResponse.createByErrorMessage("数据库不存在记录");
					}
					
				}else{
					return cf;
				}
			}else{
				return ServerResponse.createByErrorMessage("保存数据库出错");
			}
		}else{
			return ServerResponse.createByErrorMessage("必填字段不能为空");
		}
		
	}
	
	public Object bulidEmptyFile(String path,Basemsg basemsg,User user){
		mainPath=path;
		//初始化excell成功
		ServerResponse cf=(ServerResponse)this.createFile(basemsg);
		if(cf.getCode()==200){
			//
			ServerResponse sm=(ServerResponse)this.stuffMsg(basemsg,user);
			if(sm.getCode()==200){
				ServerResponse fn=(ServerResponse)this.toPDF(basemsg);
					if(fn.getCode()==200){
						return ServerResponse.createBySuccessMessage("保存成功");
					}else{
						return fn;
					}
			}else{
				return sm;
			}
			
			}else{
				return ServerResponse.createByErrorMessage("数据库不存在记录");
			}
	}
	
	
	@Override
	public List<Basemsg> getMsgByAccount(User user) {
		// TODO Auto-generated method stub
		return basemsgMapper.getMsgByAccount(user);
	}
	
	public Object createFile(Basemsg basemsg){
		return ExcellUtil.copyFile(mainPath,basemsg,"用水单位基本信息");
	}
	
	public Object stuffMsg(Basemsg basemsg,User user){
		File file=new File(mainPath+basemsg.getAccount()+"_用水单位基本信息.xlsx");
		try {
		 FileInputStream fis=new FileInputStream(file);
	     Workbook workbook= WorkbookFactory.create(fis);
	     Sheet sheet=workbook.getSheetAt(0);
	     //填表单位名称
	     if(StringUtils.isNotBlank(user.getCompany())){
	    	 String oldValue=ExcellUtil.getExcellString(sheet,0,5);
	    	 oldValue=oldValue.replace("：", "："+user.getCompany());
	    	 ExcellUtil.updateExcel(sheet,0,5,oldValue);
	     }
	     //年份
	     if(basemsg.getRecYear()!=null){
	    	 String oldValue=ExcellUtil.getExcellString(sheet,4,5);
	    	 oldValue=oldValue.replace("20  ", Integer.toString(basemsg.getRecYear()));
	    	 ExcellUtil.updateExcel(sheet,4,5,oldValue);
	     }
	     //单位编号
	     if(StringUtils.isNotBlank(basemsg.getUnitId())){
	    	 ExcellUtil.updateExcel(sheet,2,6,basemsg.getUnitId());
	     }
		 //单位名称
	     if(StringUtils.isNotBlank(basemsg.getUnitName())){
		 ExcellUtil.updateExcel(sheet,4,6,basemsg.getUnitName());
	     }
		 //年份
//	     if(basemsg.getUnitName()!=null){
//		 ExcellUtil.updateExcel(sheet,4,5,"2019年");
//	     }
		 //用户类型
		 if(StringUtils.isNotBlank(basemsg.getUnitType())){
		 ExcellUtil.updateExcel(sheet,2,7,basemsg.getUnitType());
		 }
		 //水厂名称
		 if(StringUtils.isNotBlank(basemsg.getFactoryName())){
		 ExcellUtil.updateExcel(sheet,4,7,basemsg.getFactoryName());
		 }
		 //组织机构代码
		 if(StringUtils.isNotBlank(basemsg.getOrgCode())){
		 ExcellUtil.updateExcel(sheet,6,7,basemsg.getOrgCode());
		 }
		 //所有制类别
		 if(StringUtils.isNotBlank(basemsg.getOwnership())){
		 ExcellUtil.updateExcel(sheet,2,8,basemsg.getOwnership());
		 }
		 //用户级别
		 if(StringUtils.isNotBlank(basemsg.getUserlevel())){
		 ExcellUtil.updateExcel(sheet,4,8,basemsg.getUserlevel());
		 }
		 //上级主管部门
		 if(StringUtils.isNotBlank(basemsg.getManagerDepartment())){
		 ExcellUtil.updateExcel(sheet,6,8,basemsg.getManagerDepartment());
		 }
		 //所在区
		 if(StringUtils.isNotBlank(basemsg.getDistrict())){
		 ExcellUtil.updateExcel(sheet,8,8,basemsg.getDistrict());
		 }
		 //街道（乡镇）
		 if(StringUtils.isNotBlank(basemsg.getTown())){
		 ExcellUtil.updateExcel(sheet,2,9,basemsg.getTown());
		 }
		 //国标行业
		 if(StringUtils.isNotBlank(basemsg.getGBIndustry())){
		 ExcellUtil.updateExcel(sheet,4,9,basemsg.getGBIndustry());
		 }
		//主要行业
		 if(StringUtils.isNotBlank(basemsg.getMainIndustry())){
		 ExcellUtil.updateExcel(sheet,8,9,basemsg.getMainIndustry());
		 }
		//邮政编码
		 if(StringUtils.isNotBlank(basemsg.getPostalCode())){
		 ExcellUtil.updateExcel(sheet,2,10,basemsg.getPostalCode());
		 }
		//单位地址
		 if(StringUtils.isNotBlank(basemsg.getAddr())){
		 ExcellUtil.updateExcel(sheet,4,10,basemsg.getAddr());
		 }
		//管水部门
		 if(StringUtils.isNotBlank(basemsg.getWateDepartment())){
		 ExcellUtil.updateExcel(sheet,8,10,basemsg.getWateDepartment());
		 }
		//联系人
		 if(StringUtils.isNotBlank(basemsg.getContacts())){
		 ExcellUtil.updateExcel(sheet,2,11,basemsg.getContacts());
		 }
		//联系电话
		 if(StringUtils.isNotBlank(basemsg.getTel())){
		 ExcellUtil.updateExcel(sheet,4,11,basemsg.getTel());
		 }
		//传真
		 if(StringUtils.isNotBlank(basemsg.getFax())){
		 ExcellUtil.updateExcel(sheet,6,11,basemsg.getFax());
		 }
		//电子邮件
		 if(StringUtils.isNotBlank(basemsg.getEmail())){
		 ExcellUtil.updateExcel(sheet,8,11,basemsg.getEmail());
		 }
		//单位主业性质
		 if(StringUtils.isNotBlank(basemsg.getIndustryType())){
		 ExcellUtil.updateExcel(sheet,2,12,basemsg.getIndustryType(),1);
		 }
		//总占地面积
		 if(basemsg.getAreaCovered()!=null){
		 ExcellUtil.updateExcel(sheet,2,13,Double.toString(basemsg.getAreaCovered()));
		 }
		//总建筑面积
		 if(basemsg.getAreaBuilt()!=null){
		 ExcellUtil.updateExcel(sheet,4,13,Double.toString(basemsg.getAreaBuilt()));
		 }
		//建筑占地面积
		 if(basemsg.getBuildCovered()!=null){
		 ExcellUtil.updateExcel(sheet,6,13,Double.toString(basemsg.getBuildCovered()));
		 }
		//绿化面积
		 if(basemsg.getAreaAfforested()!=null){
		 ExcellUtil.updateExcel(sheet,8,13,Double.toString(basemsg.getAreaAfforested()));
		 }
		//供冷面积
		 if(basemsg.getAreaCool()!=null){
		 ExcellUtil.updateExcel(sheet,2,14,Double.toString(basemsg.getAreaCool()));
		 }
		//供冷方式
		 if(StringUtils.isNotBlank(basemsg.getCoolType())){
			 if("1".equals(basemsg.getCoolType())){
				 ExcellUtil.updateExcel(sheet,4,14,"中央空调",1);
			 }else{
				 ExcellUtil.updateExcel(sheet,4,14,"非中央空调",1);
			 }
		 }
		//是否有中水设施
		 if(StringUtils.isNotBlank(basemsg.getRecycleDevice())){
			 if("1".equals(basemsg.getRecycleDevice())){
				 ExcellUtil.updateExcel(sheet,2,15,"是",1);
			 }else{
				 ExcellUtil.updateExcel(sheet,2,15,"否",1);
			 }
		 }
		//中水设施状态
		 if(StringUtils.isNotBlank(basemsg.getDeviceStatus())){
			 if("1".equals(basemsg.getDeviceStatus())){
				 ExcellUtil.updateExcel(sheet,4,15,"正常",1);
			 }else if("2".equals(basemsg.getDeviceStatus())){
				 ExcellUtil.updateExcel(sheet,4,15,"停用",1);
			 }else{
				 ExcellUtil.updateExcel(sheet,4,15,"在建",1);
			 }
		 }
		//中水设施处理能力
		 if(basemsg.getRecycleCapacity()!=null){
		 ExcellUtil.updateExcel(sheet,6,15,Double.toString(basemsg.getRecycleCapacity()));
		 }
		//中水设施利用量
		 if(basemsg.getRecycleUsed()!=null){
		 ExcellUtil.updateExcel(sheet,8,15,Double.toString(basemsg.getRecycleUsed()));
		 }
		//是否使用市政中水
		 if(StringUtils.isNotBlank(basemsg.getCivicismRecycle())){
			 if("1".equals(basemsg.getCivicismRecycle())){
				 ExcellUtil.updateExcel(sheet,3,16,"是",1);
			 }else{
				 ExcellUtil.updateExcel(sheet,3,16,"否",1);
			 }
		 }
		//市政中水利用量
		 if(basemsg.getCivicismUsed()!=null){
			 ExcellUtil.updateExcel(sheet,6,16,Double.toString(basemsg.getCivicismUsed()));
		 }
		//是否有雨水利用工程
		 if(StringUtils.isNotBlank(basemsg.getRainDevice())){
			 if("1".equals(basemsg.getRainDevice())){
				 ExcellUtil.updateExcel(sheet,3,17,"是",1);
			 }else{
				 ExcellUtil.updateExcel(sheet,3,17,"否",1);
			 }
		 }
		//雨水工程类型
		/* if("1".equals(basemsg.getRainDeviceType())){
			 ExcellUtil.updateExcel(sheet,6,17,"收集利用",1);
		 }else if("2".equals(basemsg.getRainDeviceType())){
			 ExcellUtil.updateExcel(sheet,6,17,"透水砖",1);
		 }else if("3".equals(basemsg.getRainDeviceType())){
			 ExcellUtil.updateExcel(sheet,6,17,"下凹式绿地",1);
		 }else{
			 ExcellUtil.updateExcel(sheet,6,17,"渗井",1);
		 }*/
		 if(StringUtils.isNotBlank(basemsg.getRainDeviceType())){
			 ExcellUtil.updateExcel(sheet,6,17,basemsg.getRainDeviceType(),1);	 
		 }
		//雨水利用工程利用量
		 if(basemsg.getRainDeviceUsed()!=null){
			 ExcellUtil.updateExcel(sheet,3,18,Double.toString(basemsg.getRainDeviceUsed()));
		 }
		//雨水利用工程蓄水能力
		 if(basemsg.getRainDeviceSpace()!=null){
			 ExcellUtil.updateExcel(sheet,7,18,Double.toString(basemsg.getRainDeviceSpace()));
		 }
		//是否节水型创建企业
		 if(StringUtils.isNotBlank(basemsg.getWaterSaveUint())){
			 if("1".equals(basemsg.getWaterSaveUint())){
				 ExcellUtil.updateExcel(sheet,4,19,"是",1);
			 }else{
				 ExcellUtil.updateExcel(sheet,4,19,"否",1);
			 }
		 }
		//创建类别
		 if(StringUtils.isNotBlank(basemsg.getSaveLevel())){
		 if("1".equals(basemsg.getSaveLevel())){
			 ExcellUtil.updateExcel(sheet,6,19,"市级",1);
		 }else{
			 ExcellUtil.updateExcel(sheet,6,19,"区级",1);
		 }
		 }
		//自取水规模
		 if(basemsg.getWaterUserLevel()!=null){
			 ExcellUtil.updateExcel(sheet,2,20,Double.toString(basemsg.getWaterUserLevel()));
		 }
		//取水许可证编号
		 if(StringUtils.isNotBlank(basemsg.getLicenseId())){
			 ExcellUtil.updateExcel(sheet,4,20,basemsg.getLicenseId());
		 }
		//应装一级水表
		 if(basemsg.getWaterMeter()!=null){
			 ExcellUtil.updateExcel(sheet,8,20,Integer.toString(basemsg.getWaterMeter()));
		 }
		//已装一级水表
		 if(basemsg.getInstallMeter()!=null){
			 ExcellUtil.updateExcel(sheet,2,21,Integer.toString(basemsg.getInstallMeter()));
		 }
		//是否已经纳入计划用水
		 if(StringUtils.isNotBlank(basemsg.getIsWaterPlan())){
			 if("1".equals(basemsg.getIsWaterPlan())){
				 ExcellUtil.updateExcel(sheet,4,21,"是",1);
			 }else{
				 ExcellUtil.updateExcel(sheet,4,21,"否",1);
			 }
		 }
//		 if(basemsg.getIsWaterPlan()!=null){
//		 ExcellUtil.updateExcel(sheet,4,21,basemsg.getIsWaterPlan());
//		 }
		 
		//社会单位合计
		 /*if(basemsg.getWaterTrap_U()!=null&&basemsg.getClosestool_U()!=null&&basemsg.getShower_U()!=null&&basemsg.getUtensils_U()!=null){
			 ExcellUtil.updateExcel(sheet,3,24,Integer.toString(basemsg.getWaterTrap_U()+basemsg.getClosestool_U()+basemsg.getShower_U()+basemsg.getUtensils_U()));
		 }*/
		 if(basemsg.getAllrow1()!=null){
			 ExcellUtil.updateExcel(sheet,3,24,Integer.toString(basemsg.getAllrow1()));
		 }
		//居民家庭合计
		 /*if(basemsg.getWaterTrap_F()!=null&&basemsg.getClosestool_F()!=null&&basemsg.getShower_F()!=null&&basemsg.getUtensils_F()!=null){
			 ExcellUtil.updateExcel(sheet,4,24,Integer.toString(basemsg.getWaterTrap_F()+basemsg.getClosestool_F()+basemsg.getShower_F()+basemsg.getUtensils_F()));
		 }*/
		 if(basemsg.getAllrow2()!=null){
			 ExcellUtil.updateExcel(sheet,4,24,Integer.toString(basemsg.getAllrow2()));
		 }
		//社会单位节水合计
		 /*if(basemsg.getSaveWaterTrap_U()!=null&&basemsg.getSaveClosestool_U()!=null&&basemsg.getSaveShower_U()!=null&&basemsg.getSaveUtensils_U()!=null){
			 ExcellUtil.updateExcel(sheet,5,24,Integer.toString(basemsg.getSaveWaterTrap_U()+basemsg.getSaveClosestool_U()+basemsg.getSaveShower_U()+basemsg.getSaveUtensils_U()));
		 }*/
		 if(basemsg.getAllrow3()!=null){
			 ExcellUtil.updateExcel(sheet,5,24,Integer.toString(basemsg.getAllrow3()));
		 }
		//居民家庭节水合计
		 /*if(basemsg.getSaveWaterTrap_F()!=null&&basemsg.getSaveClosestool_F()!=null&&basemsg.getSaveShower_F()!=null&&basemsg.getSaveUtensils_F()!=null){
			 ExcellUtil.updateExcel(sheet,6,24,Integer.toString(basemsg.getSaveWaterTrap_F()+basemsg.getSaveClosestool_F()+basemsg.getSaveShower_F()+basemsg.getSaveUtensils_F()));
		 }*/
		 if(basemsg.getAllrow4()!=null){
			 ExcellUtil.updateExcel(sheet,6,24,Integer.toString(basemsg.getAllrow4()));
		 }
		//社会单位节水型器具普及率合计
		 /*if(basemsg.getWaterTrap_U()!=null&&basemsg.getClosestool_U()!=null&&basemsg.getShower_U()!=null&&basemsg.getUtensils_U()!=null&&
				 basemsg.getSaveWaterTrap_U()!=null&&basemsg.getSaveClosestool_U()!=null&&basemsg.getSaveShower_U()!=null&&basemsg.getSaveUtensils_U()!=null){
			 DecimalFormat    df   = new DecimalFormat("######0.0");   
			 String value=df.format((double)(basemsg.getSaveWaterTrap_U()+basemsg.getSaveClosestool_U()+basemsg.getSaveShower_U()+basemsg.getSaveUtensils_U())/((double)(basemsg.getWaterTrap_U()+basemsg.getClosestool_U()+basemsg.getShower_U()+basemsg.getUtensils_U()) )*100)+"%";
			 ExcellUtil.updateExcel(sheet,7,24,  value);
		 }*/
		 if(basemsg.getAllrow1()!=null &&basemsg.getAllrow3()!=null){
			 DecimalFormat    df   = new DecimalFormat("######0.0");   
			 String value=df.format(((double)basemsg.getAllrow3())/((double)basemsg.getAllrow1() )*100)+"%";
			 ExcellUtil.updateExcel(sheet,7,24,  value);
		 }
		//居民家庭节水型器具普及率合计
		 /*if(basemsg.getSaveWaterTrap_F()!=null&&basemsg.getSaveClosestool_F()!=null&&basemsg.getSaveShower_F()!=null&&basemsg.getSaveUtensils_F()!=null&&
				 basemsg.getWaterTrap_F()!=null&&basemsg.getClosestool_F()!=null&&basemsg.getShower_F()!=null&&basemsg.getUtensils_F()!=null){
			 DecimalFormat    df   = new DecimalFormat("######0.0");   
			 String value=df.format(((double)(basemsg.getSaveWaterTrap_F()+basemsg.getSaveClosestool_F()+basemsg.getSaveShower_F()+basemsg.getSaveUtensils_F()))/((double)(basemsg.getWaterTrap_F()+basemsg.getClosestool_F()+basemsg.getShower_F()+basemsg.getUtensils_F()))*100)+"%";
			 ExcellUtil.updateExcel(sheet,8,24,value);
		 }*/
		 if(basemsg.getAllrow2()!=null &&basemsg.getAllrow4()!=null){
			 DecimalFormat    df   = new DecimalFormat("######0.0");   
			 String value=df.format(((double)basemsg.getAllrow3())/((double)basemsg.getAllrow2() )*100)+"%";
			 ExcellUtil.updateExcel(sheet,8,24,value);
		 }
		 
		//单位水龙头节水型器具普及率
		 if(basemsg.getWaterTrap_U()!=null&&basemsg.getSaveWaterTrap_U()!=null){
			 DecimalFormat    df   = new DecimalFormat("######0.0");   
			 String value=df.format(((double)basemsg.getSaveWaterTrap_U())/((double)basemsg.getWaterTrap_U())*100)+"%";
			 ExcellUtil.updateExcel(sheet,7,25,value);
		 }
		 
		//居民家庭水龙头节水型器具普及率
		 if(basemsg.getWaterTrap_F()!=null&&basemsg.getSaveWaterTrap_F()!=null){
			 DecimalFormat    df   = new DecimalFormat("######0.0");   
			 String value=df.format(((double)basemsg.getSaveWaterTrap_F())/((double)basemsg.getWaterTrap_F())*100)+"%";
			 ExcellUtil.updateExcel(sheet,8,25,value);
		 }
		 
		//单位便器水箱节水型器具普及率
		 if(basemsg.getClosestool_U()!=null&&basemsg.getSaveClosestool_U()!=null){
			 DecimalFormat    df   = new DecimalFormat("######0.0");   
			 String value=df.format(((double)basemsg.getSaveClosestool_U())/((double)basemsg.getClosestool_U())*100)+"%";
			 ExcellUtil.updateExcel(sheet,7,26,value);
		 }
		 
		//居民家庭便器水箱节水型器具普及率
		 if(basemsg.getClosestool_F()!=null&&basemsg.getSaveClosestool_F()!=null){
			 DecimalFormat    df   = new DecimalFormat("######0.0");   
			 String value=df.format(((double)basemsg.getSaveClosestool_F())/((double)basemsg.getClosestool_F())*100)+"%";
			 ExcellUtil.updateExcel(sheet,8,26,value);
		 }
		 
		 
		//单位淋浴器节水型器具普及率
		 if(basemsg.getShower_U()!=null&&basemsg.getSaveShower_U()!=null){
			 DecimalFormat    df   = new DecimalFormat("######0.0");   
			 String value=df.format(((double)basemsg.getSaveShower_U())/((double)basemsg.getShower_U())*100)+"%";
			 ExcellUtil.updateExcel(sheet,7,27,value);
		 }
		 
		//居民家庭淋浴器节水型器具普及率
		 if(basemsg.getShower_F()!=null&&basemsg.getSaveShower_F()!=null){
//			 BigDecimal   b   =   new   BigDecimal(((double)basemsg.getSaveShower_F())/((double)basemsg.getShower_F()));
//			 double temp=b.setScale(3,   BigDecimal.ROUND_HALF_UP).doubleValue();
			 DecimalFormat    df   = new DecimalFormat("######0.0");   
			 String value=df.format(((double)basemsg.getSaveShower_F())/((double)basemsg.getShower_F())*100)+"%";
			 ExcellUtil.updateExcel(sheet,8,27,value);
		 }
		 
		 
		//单位其它节水型器具普及率
		 if(basemsg.getUtensils_U()!=null&&basemsg.getSaveUtensils_U()!=null){
			 DecimalFormat    df   = new DecimalFormat("######0.0");   
			 String value=df.format(((double)basemsg.getSaveUtensils_U())/((double)basemsg.getUtensils_U())*100)+"%";
			 ExcellUtil.updateExcel(sheet,7,28,value);
		 }
		 
		//居民家庭其它节水型器具普及率
		 if(basemsg.getUtensils_F()!=null&&basemsg.getSaveUtensils_F()!=null){
			 DecimalFormat    df   = new DecimalFormat("######0.0");   
			 String value=df.format(((double)basemsg.getSaveUtensils_F())/((double)basemsg.getUtensils_F())*100)+"%";
			 ExcellUtil.updateExcel(sheet,8,28,value);
		 }
		 
		//单位水龙头数量
		 if(basemsg.getWaterTrap_U()!=null){
			 ExcellUtil.updateExcel(sheet,3,25,Integer.toString(basemsg.getWaterTrap_U()));
		 }
		//居民家庭水龙头数量
		 if(basemsg.getWaterTrap_F()!=null){
			 ExcellUtil.updateExcel(sheet,4,25,Integer.toString(basemsg.getWaterTrap_F()));
		 }
		//单位节水型水龙头数量
		 if(basemsg.getSaveWaterTrap_U()!=null){
			 ExcellUtil.updateExcel(sheet,5,25,Integer.toString(basemsg.getSaveWaterTrap_U()));
		 }
		//居民家庭节水型水龙头数量
		 if(basemsg.getSaveWaterTrap_F()!=null){
			 ExcellUtil.updateExcel(sheet,6,25,Integer.toString(basemsg.getSaveWaterTrap_F()));
		 }
		//单位便器水箱数量
		 if(basemsg.getClosestool_U()!=null){
			 ExcellUtil.updateExcel(sheet,3,26,Integer.toString(basemsg.getClosestool_U()));
		 }
		//居民家庭便器水箱数量
		 if(basemsg.getClosestool_F()!=null){
			 ExcellUtil.updateExcel(sheet,4,26,Integer.toString(basemsg.getClosestool_F()));
		 }
		//单位节水型便器水箱数量
		 if(basemsg.getSaveClosestool_U()!=null){
			 ExcellUtil.updateExcel(sheet,5,26,Integer.toString(basemsg.getSaveClosestool_U()));
		 }
		//居民家庭节水型便器水箱数量
		 if(basemsg.getSaveClosestool_F()!=null){
			 ExcellUtil.updateExcel(sheet,6,26,Integer.toString(basemsg.getSaveClosestool_F()));
		 }
		//单位淋浴器数量
		 if(basemsg.getShower_U()!=null){
			 ExcellUtil.updateExcel(sheet,3,27,Integer.toString(basemsg.getShower_U()));
		 }
		//居民家庭淋浴器数量
		 if(basemsg.getShower_F()!=null){
			 ExcellUtil.updateExcel(sheet,4,27,Integer.toString(basemsg.getShower_F()));
		 }
		//单位节水型淋浴器数量
		 if(basemsg.getSaveShower_U()!=null){
			 ExcellUtil.updateExcel(sheet,5,27,Integer.toString(basemsg.getSaveShower_U()));
		 }
		//居民家庭节水型淋浴器数量
		 if(basemsg.getSaveShower_F()!=null){
			 ExcellUtil.updateExcel(sheet,6,27,Integer.toString(basemsg.getSaveShower_F()));
		 }
		//单位其它器具数量
		 if(basemsg.getUtensils_U()!=null){
			 ExcellUtil.updateExcel(sheet,3,28,Integer.toString(basemsg.getUtensils_U()));
		 }
		//居民家庭其它器具数量
		 if(basemsg.getUtensils_F()!=null){
			 ExcellUtil.updateExcel(sheet,4,28,Integer.toString(basemsg.getUtensils_F()));
		 }
		//单位节水型其它器具数量
		 if(basemsg.getSaveUtensils_U()!=null){
			 ExcellUtil.updateExcel(sheet,5,28,Integer.toString(basemsg.getSaveUtensils_U()));
		 }
		//居民家庭节水型其它器具数量
		 if(basemsg.getSaveUtensils_F()!=null){
			 ExcellUtil.updateExcel(sheet,6,28,Integer.toString(basemsg.getSaveUtensils_F()));
		 }
		 
		//报出日期
		 if(StringUtils.isNotBlank(basemsg.getTs())){
			 String oldValue=ExcellUtil.getExcellString(sheet,0,29);
			 oldValue=oldValue.replace("20", basemsg.getTs().substring(0, 4));
	    	 oldValue=oldValue.replace("月", basemsg.getTs().substring(5, 7)+"月");
	    	 oldValue=oldValue.replace("日 ", basemsg.getTs().substring(8, 10)+"日");
			 ExcellUtil.updateExcel(sheet,0,29,oldValue);
		 }
		//单位负责人
		 if(StringUtils.isNotBlank(basemsg.getUnitHead())){
			 String oldValue=ExcellUtil.getExcellString(sheet,0,29);
			 String preValue="";
			 for(int c=0;c<basemsg.getUnitHead().length();c++){
				 preValue=preValue+" ";
			 }
	    	 oldValue=oldValue.replace("单位负责人："+preValue, "单位负责人："+basemsg.getUnitHead());
			 ExcellUtil.updateExcel(sheet,0,29,oldValue);
		 }
		//填表人
		 if(StringUtils.isNotBlank(basemsg.getOperater())){
			 String oldValue=ExcellUtil.getExcellString(sheet,0,29);
			 String preValue="";
			 for(int c=0;c<basemsg.getOperater().length();c++){
				 preValue=preValue+" ";
			 }
	    	 oldValue=oldValue.replace("填表人："+preValue, "填表人："+basemsg.getOperater());
			 ExcellUtil.updateExcel(sheet,0,29,oldValue);
		 }
		//电话
		 if(StringUtils.isNotBlank(user.getPhone())){
			 String oldValue=ExcellUtil.getExcellString(sheet,0,29);
			 String preValue="";
			 for(int c=0;c<user.getPhone().length();c++){
				 preValue=preValue+" ";
			 }
	    	 oldValue=oldValue.replace("电话："+preValue, "电话："+user.getPhone());
			 ExcellUtil.updateExcel(sheet,0,29,oldValue);
		 }
		
		 
		/*//是否已提交
		 if(basemsg.getIssubmit()!=null){
			 ExcellUtil.updateExcel(sheet,8,7,basemsg.getIssubmit());
		 }
		//时间戳
		 	ExcellUtil.updateExcel(sheet,8,7,basemsg.getTs());*/
		 fis.close();//关闭文件输入流
		 
	     FileOutputStream fos=new FileOutputStream(file);
	     workbook.write(fos);
	     fos.close();//关闭文件输出流
		 
		 return ServerResponse.createBySuccessMessage("修改成功");
		} catch (Exception e) {
			return ServerResponse.createByErrorMessage(e.getMessage());
		}
		
	}
	
	public Object toPDF(Basemsg basemsg){
		try {
			return ExcellUtil.excel2Pdf(mainPath,basemsg,"用水单位基本信息");
		} catch (Exception e) {
			return ServerResponse.createByErrorMessage(e.getMessage());
		}
	}
	
	public void delMsg(Basemsg basemsg){
		basemsgMapper.delMsg(basemsg);
		basemsgMapper.updateVersion(basemsg);
	}

	@Override
	public List<Map<String, String>> getHangyeCode() {
		return basemsgMapper.getHangyeCode();
	}

	@Override
	public List<Basemsg> getMsgByAccountExcell(Basemsg user) {
		return basemsgMapper.getMsgByAccountExcell(user);
	}

	@Override
	public Object checkCommit(User user) {
		List<Basemsg> li=this.getMsgByAccount(user);
		if(li.size()>0){
			Basemsg temp=this.getMsgByAccount(user).get(0);
			
			if(StringUtils.isBlank(temp.getUnitId())||
					temp.getRecYear()==null||
					StringUtils.isBlank(temp.getUnitName())||
					StringUtils.isBlank(temp.getUnitType())||
//					StringUtils.isBlank(temp.getFactoryName())||
					StringUtils.isBlank(temp.getOrgCode())||
					StringUtils.isBlank(temp.getOwnership())||
					StringUtils.isBlank(temp.getUserlevel())||
//					StringUtils.isBlank(temp.getManagerDepartment())||
					StringUtils.isBlank(temp.getTown())||
					StringUtils.isBlank(temp.getAddr())||
					StringUtils.isBlank(temp.getContacts())||
					StringUtils.isBlank(temp.getTel())||
					StringUtils.isBlank(temp.getIsWaterPlan())||
					StringUtils.isBlank(temp.getDistrict())||
					StringUtils.isBlank(temp.getGBIndustry())||
					StringUtils.isBlank(temp.getMainIndustry())||
					StringUtils.isBlank(temp.getIndustryType())||
					StringUtils.isBlank(temp.getCoolType())||
					StringUtils.isBlank(temp.getRecycleDevice())||
					StringUtils.isBlank(temp.getCivicismRecycle())||
					StringUtils.isBlank(temp.getRainDevice())||
					StringUtils.isBlank(temp.getWaterSaveUint())||
					temp.getAllrow1()==null||
					temp.getAllrow2()==null||
					temp.getAllrow3()==null||
					temp.getAllrow4()==null||
					StringUtils.isBlank(temp.getUnitHead())||
					StringUtils.isBlank(temp.getOperater())
					
					/*StringUtils.isBlank(temp.getUnitType())||
			StringUtils.isBlank(temp.getFactoryName())||
			StringUtils.isBlank(temp.getOrgCode())||
			StringUtils.isBlank(temp.getOwnership())||
			StringUtils.isBlank(temp.getUserlevel())||
			StringUtils.isBlank(temp.getManagerDepartment())||
			StringUtils.isBlank(temp.getDistrict())||
			StringUtils.isBlank(temp.getTown())||
			StringUtils.isBlank(temp.getAddr())||
			StringUtils.isBlank(temp.getGBIndustry())||
			StringUtils.isBlank(temp.getMainIndustry())||
			StringUtils.isBlank(temp.getPostalCode())||
			StringUtils.isBlank(temp.getWateDepartment())||
			StringUtils.isBlank(temp.getContacts())||
			StringUtils.isBlank(temp.getTel())||
			StringUtils.isBlank(temp.getFax())||
			StringUtils.isBlank(temp.getEmail())||
			StringUtils.isBlank(temp.getIndustryType())||
			temp.getAreaCovered()==null||
			temp.getAreaBuilt()==null||
			temp.getBuildCovered()==null||
			temp.getAreaAfforested()==null||
			temp.getAreaCool()==null||
			StringUtils.isBlank(temp.getCoolType())||
			StringUtils.isBlank(temp.getRecycleDevice())||
			
//				temp.getDeviceStatus()==null||
//				temp.getRecycleCapacity()==null||
//				temp.getRecycleUsed()==null||
			
			StringUtils.isBlank(temp.getCivicismRecycle())||
			
//				temp.getCivicismUsed()==null||
			StringUtils.isBlank(temp.getRainDevice())||
//				temp.getRainDeviceType()==null||
//				temp.getRainDeviceUsed()==null||
//				temp.getRainDeviceSpace()==null||
			
			temp.getWaterSaveUint()==null||
			temp.getSaveLevel()==null||
			temp.getWaterUserLevel()==null||
			temp.getLicenseId()==null||
			temp.getWaterMeter()==null||
			temp.getInstallMeter()==null||
			temp.getIsWaterPlan()==null||
			
			temp.getWaterTrap_U()==null||
			temp.getWaterTrap_F()==null||
			temp.getSaveWaterTrap_U()==null||
			temp.getSaveWaterTrap_F()==null||
			temp.getClosestool_U()==null||
			temp.getClosestool_F()==null||
			temp.getSaveClosestool_U()==null||
			temp.getSaveClosestool_F()==null||
			temp.getShower_U()==null||
			temp.getShower_F()==null||
			temp.getSaveShower_U()==null||
			temp.getUtensils_U()==null||
			temp.getUtensils_F()==null||
			temp.getSaveUtensils_U()==null||
			temp.getSaveUtensils_F()==null||
			temp.getSaveShower_F()==null*/
					){
				return ServerResponse.createByErrorMessage("用水单位基本信息内容尚未填写完成");
			}else{
				return ServerResponse.createBySuccessMessage("用水单位基本信息内容填写完成");
			}
		}else{
			return ServerResponse.createByErrorMessage("用水单位基本信息内容尚未填写完成");
		}
	}

	@Override
	public int getIfComit(User user) {
		return basemsgMapper.getIfComit(user);
	}

	@Override
	public int updateComit(User user) {
		return basemsgMapper.updateComit(user);
	}

}
